package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetCoorData;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.data.GetDockData;
import com.example.yiwei.crm.entity.BMEntity;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.readcard.MainActivitycard;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.ListAdapter;
import com.example.yiwei.crm.view.activity.customerActivity.CustomerAddActivity;
import com.example.yiwei.crm.view.activity.healthActivity.addVisitActivity;
import com.example.yiwei.crm.view.activity.menuActivity.LoadingActivity;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SimpleCoordinationActivity extends BaseActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener,View.OnClickListener{
    //悬浮按钮
    ImageView fabIconStar;
    FloatingActionMenu leftCenterMenu;
    public static LoadingActivity l;
//    FloatingActionMenu leftCenterMenu1;
    FloatingActionButton leftCenterButton;
    FloatingActionButton leftCenterButton1;
    private AutoListView lstv;
    private ListAdapter ladapter;
    private ImageView img_back, img_add,img_search;
    private TextView title;
    private Button bfbutton,btn_add_visit;
    public static SimpleCoordinationActivity s;
    List<dockingEntity> dockingList;
    List<dockingEntity> list;
    int tag=1;
    int docking_id;
    int dock_id;
    public
    int page=1;
    ImageView lcIcon1 ;
    ImageView lcIcon2 ;
    ImageView lcIcon3 ;
    ImageView lcIcon4 ;
    ImageView lcIcon5 ;
    ImageView addxz;
    ImageView addkh;
    ImageView addbf;
    AlertDialog.Builder builder;
    SharedPreferences sp;
    Button button;
    List<BMEntity> fzlist;
    List<BMEntity> abmlist;
    List<String> fzs;
    List<String> bms;
    int a = 1;
    private PopupMenu popupMenu;
    private PopupWindow mPopWindow;
    private Menu menu;
    private ImageView img_showye,img_more;
//    private RadioGroup rg;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<dockingEntity> result = (List<dockingEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    if (dockingList!=null){
                        dockingList.clear();
                    }
                    dockingList.addAll(result);
                    if (a==1){
                        ladapter = new ListAdapter(SimpleCoordinationActivity.this, dockingList);
                        lstv.setAdapter(ladapter);
                        a++;
                    }
                    break;
                case AutoListView.LOAD:
                    if (result!=null){
                        lstv.onLoadComplete();
                        dockingList.addAll(result);
                    }else {
                        lstv.onLoadComplete();
                    }

            }
//            lstv.setResultSize(result.size());
            ladapter.notifyDataSetChanged();
        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_health_education);
        img_search = (ImageView) findViewById(R.id.health_education_img_search);
        img_more= (ImageView) findViewById(R.id.simple_coording_img_more);
        img_showye= (ImageView) findViewById(R.id.simple_coording_img_shouye);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        dockingList = new ArrayList<>();
        init();
        s=this;
//        addXuanFu2();
        addXuanFu1();
//        rg= (RadioGroup) findViewById(R.id.simple_coording_rg);
        GetDockData.create();
        GetDockData.dockingEntityList = null;
        fzlist = new ArrayList<>();
        abmlist = new ArrayList<>();
        fzs = new ArrayList<>();
        bms = new ArrayList<>();
        //悬浮按钮的监听
        leftCenterMenu.setStateChangeListener(new FloatingActionMenu.MenuStateChangeListener() {
            @Override
            public void onMenuOpened(FloatingActionMenu floatingActionMenu) {
                /*
                /构建两层按钮可使两个menu的点击按钮重合，当点击外层按钮用open方法使内层按钮展开，关闭同理
                 */
//                leftCenterMenu1.open(true);
                // 增加按钮中的+号图标顺时针旋转45度
//                // Rotate the icon of rightLowerButton 45 degrees clockwise
//                icon.setRotation(0);
//                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 45);
//                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(icon, pvhR);
//                animation.start();
//                fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.jiahao2));
//                leftCenterButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yuan2));
                //按钮的初始角度
                fabIconStar.setRotation(0);
                //按钮的旋转角度
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 45);
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(fabIconStar, pvhR);
                animator.start();

            }

            @Override
            public void onMenuClosed(FloatingActionMenu floatingActionMenu) {
//                leftCenterMenu1.close(true);
                fabIconStar.setRotation(45);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 0);
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(fabIconStar, pvhR);
                animator.start();
            }
        });
        lcIcon1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        dockingList = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (dockingList != null) {
//                                    btn_add_visit.setVisibility(View.VISIBLE);
                                    ladapter = new ListAdapter(SimpleCoordinationActivity.this, dockingList);
                                    lstv.setAdapter(ladapter);
                                    a++;
                                    ladapter.notifyDataSetChanged();
                                } else {
                                    Toast.makeText(SimpleCoordinationActivity.this, "此选项没有信息", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }).start();

            }
        });
        lcIcon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            object.put("buttontype", "mycooperation");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        dockingList = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (dockingList != null) {
//                                    btn_add_visit.setVisibility(View.VISIBLE);
                                    ladapter = new ListAdapter(SimpleCoordinationActivity.this, dockingList);
                                    lstv.setAdapter(ladapter);
                                    ladapter.notifyDataSetChanged();
                                } else {
                                    Toast.makeText(SimpleCoordinationActivity.this, "此选项没有信息", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }).start();

            }
        });
        lcIcon3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            object.put("buttontype", "recyclecooperations");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        dockingList = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
//                        Log.d("aaa",dockingList.get(0).getDocking_name());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (dockingList != null) {
//                                    btn_add_visit.setVisibility(View.VISIBLE);
                                    ladapter = new ListAdapter(SimpleCoordinationActivity.this, dockingList);
                                    lstv.setAdapter(ladapter);
                                    ladapter.notifyDataSetChanged();
                                } else {
                                    lstv.setVisibility(View.GONE);
                                    Toast.makeText(SimpleCoordinationActivity.this, "此选项没有信息", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }).start();
            }
        });
        lcIcon4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SimpleCoordinationActivity.this, ClockActivty.class);
                startActivity(intent);
            }
        });
        lcIcon5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SimpleCoordinationActivity.this, ClockDetailedActivity.class);
                startActivity(intent);
            }
        });
        title.setText("协作人");
//        bfbutton.setVisibility(View.GONE);
        lstv.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        lstv.setDividerHeight(1);
        lstv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lstv.setOnRefreshListener(this);
        lstv.setOnLoadListener(this);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                String json = object.toString();
                dockDao dockDao = new dockDaoImpl();
                dockingList = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
//                Log.d("aa",dockingList.toString());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//

                            if (dockingList != null) {
//                            btn_add_visit.setVisibility(View.VISIBLE);
//                            LoadingActivity.l.finish();
                            ladapter = new ListAdapter(SimpleCoordinationActivity.this, dockingList);
                            lstv.setAdapter(ladapter);
                            ladapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(SimpleCoordinationActivity.this, "没有信息", Toast.LENGTH_SHORT).show();
                        }
//                        loadData(AutoListView.REFRESH);
                    }
                });
            }
        }).start();
        lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                dock_id = dockingList.get(i - 1).getDocking_id();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        dockDao dockDao=new dockDaoImpl();
                        GetDock.create();
                        GetDock.dockingEntity = dockingList.get(i - 1);
                        if (!GetDock.dockingEntity.toString().equals("")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent(SimpleCoordinationActivity.this, DetailedCoordinationActivity.class);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                }).start();
            }
        });
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SimpleCoordinationActivity.this, SearchCoordinationActivity.class);
                startActivity(intent);
            }
        });

                popupMenu = new PopupMenu(SimpleCoordinationActivity.this, findViewById(R.id.simple_coording_img_more));
                menu = popupMenu.getMenu();
                final MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.munu_simple_coording, menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.simple_coord_add:
                                Intent intent = new Intent(SimpleCoordinationActivity.this, CoordinationAddActivity.class);
                                startActivity(intent);
                                break;
                            case R.id.simple_coord_add_customer:
                                final List<Integer> list1= ListAdapter.getIDs();
                                for (int i=0;i<list1.size();i++){
                                    docking_id=list1.get(i);
                                }
                                if (list1.size()==1) {
                                    AlertDialog.Builder builder;
                                    builder = new AlertDialog.Builder(new ContextThemeWrapper(SimpleCoordinationActivity.this, R.style.custom_dialog));
                                    builder.setTitle("请选择信息录入方式");
                                    builder.setPositiveButton("手动输入", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Intent intent = new Intent(SimpleCoordinationActivity.this, CustomerAddActivity.class);
                                                intent.putExtra("dockid", docking_id);
                                                Toast.makeText(SimpleCoordinationActivity.this, "dock"+docking_id, Toast.LENGTH_SHORT).show();
                                                intent.putExtra("TAG", "coor");
//                                                startActivity(intent);

                                        }
                                    });
                                    builder.setNegativeButton("仪器扫描", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Intent intent = new Intent(SimpleCoordinationActivity.this, MainActivitycard.class);
                                                intent.putExtra("dockid", docking_id);
                                                Log.d("aadddcc",docking_id+"");
                                                intent.putExtra("TAG", "coor");
                                                startActivity(intent);
                                        }
                                    });
                                    builder.show();
                                    mPopWindow.dismiss();
                                }else if (list1.size() == 0) {
                                    Toast.makeText(SimpleCoordinationActivity.this, "请选择协作人！", Toast.LENGTH_SHORT).show();
                                } else if (list1.size() > 1) {
                                    Toast.makeText(SimpleCoordinationActivity.this, "只能选择一个协作人！", Toast.LENGTH_SHORT).show();
                                }
                                break;
                            case R.id.simple_coord_add_visit:
                                Intent intent1=new Intent(SimpleCoordinationActivity.this,addVisitActivity.class);
                                final List<Integer> list2= ListAdapter.getIDs();
                               for (int i=0;i<list2.size();i++){
                                   docking_id=list2.get(i);
                              }
                             if (list2.size()==1){
                                intent1.putExtra("docking_id",docking_id);
                                 Log.d("cccccc",docking_id+"");
                                intent1.putExtra("TAG","coordination");
                               startActivity(intent1);
                            }else if (list2.size()==0){
                                Toast.makeText(SimpleCoordinationActivity.this, "请选择协作人！", Toast.LENGTH_SHORT).show();
                            }else if (list2.size()>1){
                                Toast.makeText(SimpleCoordinationActivity.this, "只能选择一个协作人！", Toast.LENGTH_SHORT).show();
                             }
                                break;
                        }


                        return false;
                    }
                });
        img_showye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();

            }
        });

        img_more.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (leftCenterMenu.isOpen()){
            leftCenterMenu.close(true);
        }
        showPopupWindow();

//        if (tag==1){
//            img_more.setImageDrawable(getResources().getDrawable(R.drawable.more1));
//            tag=2;
//        }
//        if (tag==2){
//            img_more.setImageDrawable(getResources().getDrawable(R.drawable.more2));
//            tag=1;
//        }
    }
});

    }
    private void init() {
        title = (TextView) findViewById(R.id.simple_health_title);
//        bfbutton = (Button) findViewById(R.id.simple_health_education_btn_add_activity);
        img_back = (ImageView) findViewById(R.id.health_education_img_back);
        lstv = (AutoListView) findViewById(R.id.simple_health_education_listview);
    }
    public void  addXuanFu1(){
        /**
         * 一个FloatingActionButton有一个FloatingActionMenu，FloatingActionMenu添加动画弹出的子菜单。
         * FloatingActionButton的按钮点击事件将触发、弹出FloatingActionMenu中包含的子菜单。
         * FloatingActionMenu使用attachTo方法附着在一个FloatingActionButton上。
         * FloatingActionMenu在attachTo到一个FloatingActionButton后，两者之间发生关联。
         * FloatingActionMenu在添加子菜单时候，首先需要一个SubActionButton.Builder，该SubActionButton.
         * Builder通过setContentView(ImageView
         * image).build()把一个ImageView创建生产一个SubActionButton ,
         * 然后通过FloatingActionMenu.Builder的add方法把前面生成的SubActionButton添加进去。
         *
         *
         * 按钮的旋转动画在onMenuOpened和onMenuClosed中做。
         *
         */
        WindowManager wm = (WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        //按钮的大小
        int redActionButtonSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_size1);
        //距离下边的长度
        int redActionButtonMargin = 7;
        //距离右边的长度
        int redActionButtonMarginRight = width/2-getResources().getDimensionPixelSize(R.dimen.red_action_button_content_size)/2;
        //按钮中内容的大小
        int redActionButtonContentSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_size);
        int redActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.red_action_button_content_margin);
        //弹出圆弧的半径
        int redActionMenuRadius = getResources().getDimensionPixelSize(R.dimen.red_action_menu_radius2);
        int blueSubActionButtonSize = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_size);
        int blueSubActionButtonContentMargin = getResources()
                .getDimensionPixelSize(R.dimen.blue_sub_action_button_content_margin);

        fabIconStar = new ImageView(this);
//        if (leftCenterMenu.isOpen()){
//            fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.jiahao2));
//        }else{
//            //按钮中的图片
            fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.plus5));
//        }


        FloatingActionButton.LayoutParams starParams = new FloatingActionButton.LayoutParams(redActionButtonSize,
                redActionButtonSize);
        starParams.setMargins(redActionButtonMargin, redActionButtonMargin, redActionButtonMarginRight,
                redActionButtonMargin);
        fabIconStar.setLayoutParams(starParams);

        FloatingActionButton.LayoutParams fabIconStarParams = new FloatingActionButton.LayoutParams(
                redActionButtonContentSize, redActionButtonContentSize);
        fabIconStarParams.setMargins(redActionButtonContentMargin, redActionButtonContentMargin,
                redActionButtonContentMargin, redActionButtonContentMargin);
//        if (leftCenterMenu.isOpen()){
//            leftCenterButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yuan2));
//        }else{
//            leftCenterButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yuan1));
//        }
        leftCenterButton = new FloatingActionButton.Builder(this)
                .setContentView(fabIconStar, fabIconStarParams)
                .setBackgroundDrawable(R.drawable.button_action_red_selector)// 设置变化时的背景色
                .setPosition(FloatingActionButton.POSITION_BOTTOM_RIGHT)// 靠近右边和下边
                .setLayoutParams(starParams).build();

        // Set up customized SubActionButtons for the right center menu
        SubActionButton.Builder lCSubBuilder = new SubActionButton.Builder(this);
        lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_action_blue_selector));

        FrameLayout.LayoutParams blueContentParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        blueContentParams.setMargins(blueSubActionButtonContentMargin, blueSubActionButtonContentMargin,
                blueSubActionButtonContentMargin, blueSubActionButtonContentMargin);
        lCSubBuilder.setLayoutParams(blueContentParams);
        // Set custom layout params
        FrameLayout.LayoutParams blueParams = new FrameLayout.LayoutParams(blueSubActionButtonSize,
                blueSubActionButtonSize);
        lCSubBuilder.setLayoutParams(blueParams);

//        ImageView lcIcon1 = new ImageView(this);
//        ImageView lcIcon2 = new ImageView(this);
//        ImageView lcIcon3 = new ImageView(this);
//
//        lcIcon1.setImageDrawable(getResources().getDrawable(R.drawable.u116));
//        lcIcon2.setImageDrawable(getResources().getDrawable(R.drawable.u117));
//        lcIcon3.setImageDrawable(getResources().getDrawable(R.drawable.u118));
        //添加按钮
        lcIcon1 = new ImageView(this);
        lcIcon2 = new ImageView(this);
        lcIcon3 = new ImageView(this);
        lcIcon4 = new ImageView(this);
        lcIcon5 = new ImageView(this);
//         lcIcon4 = new TextView(this);
        lcIcon1.setImageDrawable(getResources().getDrawable(R.drawable.all));
        lcIcon2.setImageDrawable(getResources().getDrawable(R.drawable.mine));
        lcIcon3.setImageDrawable(getResources().getDrawable(R.drawable.huishouzhan));
//        lcIcon4.setText("选择部门");
        lcIcon4.setImageDrawable(getResources().getDrawable(R.drawable.clock3));
        lcIcon5.setImageDrawable(getResources().getDrawable(R.drawable.record1));
        // Build another menu with custom options
        leftCenterMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(lCSubBuilder.setContentView(lcIcon1, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon2, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon3, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon4, blueContentParams).build())
                .addSubActionView(lCSubBuilder.setContentView(lcIcon5, blueContentParams).build())
                .setRadius(redActionMenuRadius).setStartAngle(220).setEndAngle(320).attachTo(leftCenterButton).build();



    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            FloatingActionMenu v = leftCenterMenu;
            if (isShouldHideKeyboard(v, ev)) {
//                Toast.makeText(this, aBoolean+"", Toast.LENGTH_SHORT).show();
                if (leftCenterMenu.isOpen()){
                    leftCenterMenu.close(true);
//                    fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.jiahao1));
//                    leftCenterButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yuan1));
                }else{


                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 根据控件所在坐标和用户点击的坐标相对比，来判断是否隐藏，因为当用户点击控件时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideKeyboard(FloatingActionMenu v, MotionEvent event) {
        if (v != null ) {
            int[] l = {100, 100};
            int left = l[0],
                    top = l[1],
                    bottom = top ,
                    right = left ;
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击控件的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是控件则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.pop_computer:{
                Intent intent = new Intent(SimpleCoordinationActivity.this, CoordinationAddActivity.class);
                startActivity(intent);
                mPopWindow.dismiss();
            }
            break;
            case R.id.pop_financial:
                Intent intent1=new Intent(SimpleCoordinationActivity.this,addVisitActivity.class);
                final List<Integer> list2= ListAdapter.getIDs();
                for (int i=0;i<list2.size();i++){
                    docking_id=list2.get(i);
                }
                if (list2.size()==1){
                    intent1.putExtra("docking_id",docking_id);
                    intent1.putExtra("TAG","coordination");
                    startActivity(intent1);
                }else if (list2.size()==0){
                    Toast.makeText(SimpleCoordinationActivity.this, "请选择协作人！", Toast.LENGTH_SHORT).show();
                }else if (list2.size()>1){
                    Toast.makeText(SimpleCoordinationActivity.this, "只能选择一个协作人！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.pop_manage:{
                final List<Integer> list1= ListAdapter.getIDs();
                for (int i=0;i<list1.size();i++){
                    docking_id=list1.get(i);
                }
                if (list1.size()==1) {
                    AlertDialog.Builder builder;
                    builder = new AlertDialog.Builder(new ContextThemeWrapper(SimpleCoordinationActivity.this, R.style.custom_dialog));
                    builder.setTitle("请选择信息录入方式");
                    builder.setPositiveButton("手动输入", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(SimpleCoordinationActivity.this, CustomerAddActivity.class);
                            final List<Integer> list1 = ListAdapter.getIDs();
                            for (int i = 0; i < list1.size(); i++) {
                                docking_id = list1.get(i);
                            }
                            if (list1.size() == 1) {
                                intent.putExtra("dockid", docking_id);
                                intent.putExtra("TAG", "coor");
                                startActivity(intent);
                            }
                        }
                    });
                    builder.setNegativeButton("仪器扫描", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(SimpleCoordinationActivity.this, MainActivitycard.class);
                            final List<Integer> list1 = ListAdapter.getIDs();
                            for (int i = 0; i < list1.size(); i++) {
                                docking_id = list1.get(i);
                            }
                            if (list1.size() == 1) {
                                intent.putExtra("dockid", docking_id);
                                intent.putExtra("TAG", "coor");
                                startActivity(intent);
                            }
                        }
                    });
                    builder.show();
                    mPopWindow.dismiss();
                }else if (list1.size() == 0) {
                    Toast.makeText(SimpleCoordinationActivity.this, "请选择协作人！", Toast.LENGTH_SHORT).show();
                } else if (list1.size() > 1) {
                    Toast.makeText(SimpleCoordinationActivity.this, "只能选择一个协作人！", Toast.LENGTH_SHORT).show();
                }

            }
            break;
            case R.id.pop_back:
                Intent intent = new Intent(SimpleCoordinationActivity.this,CoorCustomerActivity.class);
                final List<Integer> list3= ListAdapter.getIDs();
                for (int i=0;i<list3.size();i++){
                    docking_id=list3.get(i);
                }
                if (list3.size()==1){
                    intent.putExtra("dockid",docking_id);
                    intent.putExtra("TAG","coordination");
                    startActivity(intent);
                }else if (list3.size()==0){
                    Toast.makeText(SimpleCoordinationActivity.this, "请选择协作人！", Toast.LENGTH_SHORT).show();
                }else if (list3.size()>1){
                    Toast.makeText(SimpleCoordinationActivity.this, "只能选择一个协作人！", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }
    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
            UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
            LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            SC sc = new SC();
            JSONObject object =sc.storage(sp);
            String json = object.toString();
            dockDao dockDao = new dockDaoImpl();
            list= dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url+1, json);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (list!=null){
                if (lstv.getVisibility()==View.GONE){
                    lstv.setVisibility(View.VISIBLE);
                }
                msg.obj = list;
                handler.sendMessage(msg);
            }else {
                lstv.setVisibility(View.GONE);
                Toast.makeText(SimpleCoordinationActivity.this, "无数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            customerDao customerdao = new customerDaoImpl();
            page=page+1;
            SC sc = new SC();
            JSONObject object =sc.storage(sp);
            String json = object.toString();
            dockDao dockDao = new dockDaoImpl();
            list= dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url+page, json);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
           if (list!=null){
               msg.obj = list;
               handler.sendMessage(msg);
           }else {
               msg.obj = list;
               handler.sendMessage(msg);
               Toast.makeText(SimpleCoordinationActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
           }
            super.onPostExecute(integer);
        }
    }
    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetCoorData.get()!=null){
            dockingList = GetCoorData.get();
            ladapter = new ListAdapter(SimpleCoordinationActivity.this, dockingList);
            lstv.setAdapter(ladapter);
            ladapter.notifyDataSetChanged();
            GetCoorData.dockingEntityList = null;
        }else{
            onRefresh();
        }

    }
    private void showPopupWindow() {
        View contentView = LayoutInflater.from(SimpleCoordinationActivity.this).inflate(
                R.layout.popuplayout, null);

        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight( LinearLayout.LayoutParams.WRAP_CONTENT);

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();    //  获取测量后的宽度
        int popupHeight = contentView.getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
        TextView tv4 = (TextView)contentView.findViewById(R.id.pop_back);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        img_more.getLocationOnScreen(location);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.pop_bg));
        mPopWindow.setTouchable(true);
        mPopWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        mPopWindow.setFocusable(true);
        mPopWindow.update();

        mPopWindow.showAtLocation(img_more, Gravity.NO_GRAVITY, (location[0] + img_more.getWidth() / 2) - popupWidth / 2, location[1] - img_more.getHeight()/2-popupHeight);

    }
}
